<?php

namespace app\common\model;

use think\Model;

class Question extends Model
{
    public function profile()
    {
        return $this->hasOne('sort', 'Id', 'type');
    }

    public function getone($id)
    {
        $info = $this->where('Id', $id)->with('profile')->find();
        if ($info) {
            return $info->toArray();
        }
        return false;
    }
    public function look($id)
    {
        $this->where('Id', $id)->setInc('look', 1);
    }
    //获取问题列表
    public function get_list($id, $page)
    {

        $z_sort = Sort::field('Id')->where('fid', $id)->select();
        if (count($z_sort) > 0) {
            $sql_text = '';
            foreach ($z_sort as $s) {
                $sql_text = $sql_text . $s['Id'] . ',';
            }
            $list = $this->where('type', 'in', $sql_text . $id)->order('Id desc')->paginate($page);
        } else {
            $list = $this->where('type', $id)->order('Id desc')->paginate($page);
        }

        return $list;
    }
    //最热获取
    public function get_hotList($id = 0)
    {
        if ($id == 0) {
            $list = $this->order('look', 'desc')->limit(10)->select();
        } else {
            $list = $this->where('type', $id)->order('look', 'desc')->limit(10)->select();
        }
        return $list;
    }
    //随机列表获取
    public function get_rand()
    {
        $max = $this->count();
        $ids = array();
        if ($max < 1) {
            $list = array();
            return $list;
        }
        for ($x = 0; $x <= 10; $x++) {
            $id = mt_rand(1, $max);
            array_push($ids, $id);
        }

        $list = $this->all($ids);
        return $list;
    }
    //搜索试题
    public function search($text)
    {
        $list = $this->where('title', 'like', '%' . $text . '%')->limit(50)->select();
        return $list;
    }
    //后台 - 题库数据列表
    //获取分类列表数据
    public function getlist($page, $limit, $key)
    {
        if (isset($key)) {
            $cate_list = $this->where('title', 'like', "%$key%")->limit($page, $limit)->with('profile')->order('Id', 'desc')->select();
            $list_number = $this->where('title', 'like', "%$key%")->count();
        } else {
            $cate_list = $this->limit($page, $limit)->with('profile')->order('Id', 'desc')->select();
            $list_number = $this->count();
        }
        $msg = '';
        if ($cate_list->isEmpty()) {
            $msg = '暂无数据';
        }
        return json_l($cate_list->toArray(), $list_number, $msg);
    }
    //更新
    public function up($data, $id)
    {
        if ($this->save($data, ['Id' => $id])) {
            return true;
        } else {
            return false;
        }
    }
    //添加
    public function add($data)
    {
        //检测是否已存在
        if ($this->where('title', $data['title'])->find()) {
            return false;
        }
        $data['mark'] = md5(date('YmdHis').mt_rand(1, 99999).microtime());
        if ($this->allowField(true)->isUpdate(false)->save($data)) {
            return true;
        } else {
            return false;
        }
    }
    //批量添加
    public function adds($data)
    {
        $ok = 0;
        $list = explode("|||", $data['option']);
        foreach ($list as $key => $value) {
            $list_s = explode("###", $value);
            if (count($list_s) != 3) {
                break;
            }
            $sre['title'] = $list_s[0];
            $sre['option'] = $list_s[1];
            $sre['answer'] = $list_s[2];
            $sre['type'] = $data['type'];
            $sre['price'] = $data['price'];
            $sre['mark'] = md5(date('YmdHis').mt_rand(1, 99999).microtime());
            if ($this->isUpdate(false)->save($sre)) {
                $ok = $ok + 1;
            }
        }
        return $ok;
    }
}
